import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'

const app = createApp(App)

app.use(createPinia())
app.use(router)
app.use(ElementPlus)
import { useCounterStore } from '@/stores/counter';
// 获取Pinia中的store实例
const counterStore = useCounterStore();
// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  //const isLoggedIn = localStorage.getItem('token') // 这里需要你实现一个函数来检查用户是否登录，比如查看本地存储中是否有登录凭证等
  const isLoggedIn = counterStore.getToken()!== null;
  console.log('isLoggedIn：',isLoggedIn)
  console.log('to.path：',to)
  if (to.path != '/login' && !isLoggedIn) {
		// 如果要访问的路由需要登录且用户未登录，则跳转到登录页面
		next({ name: 'Login' })
	} else {
		next()
	}
})
// 添加一个 afterEach 钩子来确认路由是否成功切换
router.afterEach((to, from) => {
  console.log('Route changed to:', to.path);
});

app.use(ElementPlus, {
  locale: zhCn,
})

app.mount('#app')